import { gp } from '@vui'
import isEmpty from 'lodash/isEmpty'
import cloneDeep from 'lodash/cloneDeep'
import { translateTitle } from '@/utils/i18n'
//必须带done回调 执行完成调用
export default function getTableDataByKey({ vm, action, done, eventName, callbackData }) {
  let tableId = action.extension.tableId.value && action.extension.tableId.value[0]
  const matchField = action.extension.matchField.value

  if (tableId && matchField) {
    let _tableSelf = vm.vuiComponents[tableId].self
    let _valueList = cloneDeep(_tableSelf.info.data.requestData.data)
    let rowData = {}
    !isEmpty(_valueList) &&
      _valueList.forEach((item) => {
        if (item[matchField] === callbackData[matchField]) {
          rowData = item
        }
      })
    // 存储动作数据
    vm.$store.dispatch('actionDatas/setActionsData', {
      moduleId: vm.info._id,
      actionId: action._id,
      actionData: rowData,
    })
    done(null, rowData)
  } else {
    gp.$baseMessage(`${translateTitle('Ve.eventTask.没有找到组件或没有匹配字段') - 1001}`, 'warning')
    // 修改任务队列执行状态，防止下一次事件被拦截
    vm[`${eventName}IsRunning`] = false
    done(null, {})
  }
}
